import java.util.Arrays;

public class selectSort {
    public static void selectSort1(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arrays.length; j++) {
                if(arrays[minIndex] > arrays[j]){
                    minIndex = j;
                }
            }
            if(minIndex != i){
                int temp = arrays[minIndex];
                arrays[minIndex] = arrays[i];
                arrays[i] = temp;
            }
        }
    }
    public static void testSelectSort1(int[] arrays){
        arrays = Arrays.copyOf(arrays,arrays.length);
        long start = System.currentTimeMillis();
        //System.out.println(Arrays.toString(arrays));
        selectSort1(arrays);
        //System.out.println(Arrays.toString(arrays));
        long end = System.currentTimeMillis();
        System.out.println("选择排序1所花费的时间为：" + (end - start));
    }

    public static void selectSort2(int[] arrays){
        int left = 0;
        int right = arrays.length - 1;
        while(left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if(arrays[minIndex] > arrays[i]){
                    minIndex = i;
                }
                if(arrays[maxIndex] < arrays[i]){
                    maxIndex = i;
                }
            }
            Public.swap(arrays,left,minIndex);
            if(left == maxIndex){
                maxIndex = minIndex;
            }
            Public.swap(arrays,right,maxIndex);
            left ++;
            right --;
        }
    }

    public static void testSelectSort2(int[] arrays){
        arrays = Arrays.copyOf(arrays,arrays.length);
        long start = System.currentTimeMillis();
        //System.out.println(Arrays.toString(arrays));
        selectSort2(arrays);
        //System.out.println(Arrays.toString(arrays));
        long end = System.currentTimeMillis();
        System.out.println("选择排序2所花费的时间为：" + (end - start));
    }

    public static void main(String[] args) {
        int[] arrays = new int[10_0000];

        Public.func1(arrays);
        testSelectSort1(arrays);
        testSelectSort2(arrays);

        Public.func2(arrays);
        testSelectSort1(arrays);
        testSelectSort2(arrays);

        Public.func3(arrays);
        testSelectSort1(arrays);
        testSelectSort2(arrays);


    }
}
